package com.liu.qqclient.view;

import com.liu.qqclient.Service.FileClientService;
import com.liu.qqclient.Service.MessageClientServer;
import com.liu.qqclient.Service.UserClientService;
import com.liu.qqclient.utils.Utility;

/**
 * 客户端的菜单界面
 */
public class QQView {
    private boolean loop = true;//控制是否退出循环的变量
    private String key = " ";//接收用户输入
    private UserClientService userClientService = new UserClientService();//用于登录服务器/注册用户
    private MessageClientServer messageClientServer = new MessageClientServer();//用于私发消息/群聊
    private FileClientService fileClientService = new FileClientService();//用于传输文件

    public static void main(String[] args) {
         new QQView().mainMenu();
        System.out.println("客户端退出。。。。");
    }
    //显示主菜单
    private void mainMenu(){
        while (loop){
            System.out.println("============欢迎登录网络通讯系统=============");
            System.out.println("\t\t 1: 登录系统");
            System.out.println("\t\t 9: 退出系统");
            System.out.println("请输入你的选择");
        key = Utility.readString(1);

        //根据用户的输入处理不同的逻辑
            switch (key){
                case "1":
                    System.out.println("请输入用户号：");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密  码");
                    String pwd = Utility.readString(50);
                    //需要去服务端验证，为了测试
                    if(userClientService.checkUser(userId,pwd)){
                        System.out.println("============欢迎 "+userId +"=============");
                        //进入二级菜单
                        while(loop){
                            System.out.println("\n===========网络通信系统二级菜单(用户" +  userId + ")=================");
                            System.out.println("\t\t 1:显示在线用户列表");
                            System.out.println("\t\t 2:群发消息");
                            System.out.println("\t\t 3:私聊消息");
                            System.out.println("\t\t 4:发送文件");
                            System.out.println("\t\t 9:退出系统");
                            System.out.println("请输入你的选择");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    //编写一个方法获取在线列表、
                                    userClientService.onlineFriendList();
                                break;
                                case "2":
                                    System.out.println("请输入想对大家说的话：");
                                    String allContent = Utility.readString(50);
                                    //调用一个方法，将消息封装成Message的对象，发送给服务端
                                    messageClientServer.sendMessageAll(allContent,userId);
                                break;
                                case "3":
                                    System.out.print("请输入想聊天的用户名（在线）：");
                                    String getterId = Utility.readString(50);
                                    System.out.print("请输入想说的话：");
                                    String content  = Utility.readString(50);
                                    messageClientServer.sendMessageToOne(content,userId,getterId);
                                    break;
                                case "4":
                                    System.out.print("请输入你想把文件发送给的用户（在线用户）：");
                                    getterId =  Utility.readString(100);
                                    System.out.print("请输入发送文件的路径(形式D:xx)：");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入把文件发送给对应的路径(形式D:xx):");
                                    String data = Utility.readString(100);
                                    fileClientService.sendFileToOne(src,data,userId,getterId);

                                break;
                                case "9":
                                    loop = false;
                                    userClientService.offlineFriendList();
                                break;
                            }
                        }
                    }else {
                        System.out.println("===============登录失败==============");
                    }
                break;
                case "9":
                    loop = false;
                break;
                default:
            }
        }
    }
}
